package tcp.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.bind.JAXBException;

import theMarshall.TaskMarshallUtil;

public class TcpClient {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws JAXBException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, JAXBException {
        try {
            // IP address of the server,
            InetAddress serverAddress = InetAddress.getByName("localhost");
            
            // It is the same port where server will be listening.
            int serverPort = 7898;
            
            String message = "Hello Server!";
            // Open a socket for communication.
            Socket socket = new Socket(serverAddress, serverPort);
            
            // Get data output stream to send a String message to server.
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            dos.writeUTF(message);
            
            dos.flush();
            
            // Now switch to listening mode for receiving message from server.
            DataInputStream dis = new DataInputStream(socket.getInputStream());
            
           // Note that this is a blocking call,  
            String response = dis.readUTF();
            
            
            // Print the message.
            System.out.println("Message from Server: " + response);
            

            ObjectOutputStream toServer;
            ObjectInputStream fromServer;
            
            toServer = new ObjectOutputStream(socket.getOutputStream());
            fromServer = new ObjectInputStream(socket.getInputStream());
            
            
         
            TaskMarshallUtil marsh = (TaskMarshallUtil) fromServer.readObject();
            marsh.PrintTaskObject();
            marsh.close();
            // Finnaly close the socket. 
            socket.close();
            
        } catch (IOException ex) {
            Logger.getLogger(TcpClient.class.getName()).log(Level.SEVERE, null, ex);
            
            System.out.println("error message: " + ex.getMessage());
        } 
        
        
    
		// TODO Auto-generated method stub

	}

}
